﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Net;
using System.Text.RegularExpressions;


namespace Gammar.Digger.SystemFramework
{
    public sealed class WebHelper
    {
        #region get html source code
        public byte[] GetSource(string pageUrl)
        {
            WebClient wc = new WebClient();
            Byte[] pageData;
            try
            {
                Console.WriteLine("Reading:"+pageUrl);
                pageData = wc.DownloadData(pageUrl);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("WebHelper:GetSource:::" + ex.Message);
                Console.ForegroundColor = ConsoleColor.White;
                pageData = new Byte[0];
            }
          
            wc.Dispose();
            return pageData;
        }

        public string GetSource(string pageUrl, Encoding encoding)
        {
            if (encoding != null)
            {
                return encoding.GetString(GetSource(pageUrl));
            }
            else
            {
                return Encoding.Default.GetString(GetSource(pageUrl));
            }

        }

        public string DownHtml(string url,Encoding encoding)
        {
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
            System.Net.WebResponse response = request.GetResponse();
            System.IO.Stream resStream = response.GetResponseStream();
            System.IO.StreamReader sr = new System.IO.StreamReader(resStream, System.Text.Encoding.Default);
            string htmlCode = (sr.ReadToEnd());
            resStream.Close();
            sr.Close();
            return htmlCode;
        }

        #endregion

        /// <summary>
        /// 匹配字符串
        /// </summary>
        /// <param name="sourceData"></param>
        /// <returns></returns>
        public List<string> MathString(string sourceData, string pattern)
        {
            List<string> strArray = new List<string>();
            try
            {
                MatchCollection matchs = new Regex(pattern, RegexOptions.Compiled).Matches(sourceData);
                if (matchs.Count != 0)
                {
                    for (int i = 0; i < matchs.Count; i++)
                    {
                        strArray.Add(matchs[i].Groups[1].Value);
                    }

                }
            }
            catch (Exception ex)
            {

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("WebHelper:MathString:::" + ex.Message);
                Console.ForegroundColor = ConsoleColor.White;
            }

            return strArray;

        }

        public string MathImage(string sourceData)
        {
            return string.Empty;
        }

        /// <summary>
        /// 格式化采集到的url
        /// </summary>
        /// <param name="BaseUrl">插件根url</param>
        /// <param name="theUrl">待格式化的url</param>
        /// <returns>格式化后的url</returns>
        public string FormatUrl(string BaseUrl, string theUrl)
        {
            int num = 0;
            //theUrl = theUrl;
            string str = BaseUrl.Substring(0, BaseUrl.IndexOf("/", 8));
            string BasePage=string.Empty;
            if (BaseUrl.Contains("?"))
            {
                BasePage = BaseUrl.Substring(0, BaseUrl.IndexOf("?"));
            }
            else
            {
                BasePage = BaseUrl;
            }
           
            BaseUrl = BaseUrl.Substring(0, BaseUrl.LastIndexOf("/"));
            
            if (theUrl.StartsWith("./"))
            {
                theUrl = theUrl.Remove(0, 1);
                theUrl = BaseUrl + theUrl;
            }
            else if (theUrl.StartsWith("/"))
            {
                theUrl = str + theUrl;
            }
            else if (theUrl.StartsWith("../"))
            {
                while (theUrl.StartsWith("../"))
                {
                    num = ++num;
                    theUrl = theUrl.Remove(0, 3);
                }
                for (int i = 0; i < num; i++)
                {
                    BaseUrl = BaseUrl.Substring(0, BaseUrl.LastIndexOf("/", (int)(BaseUrl.Length - 2)));
                }
                theUrl = BaseUrl + "/" + theUrl;
            }
            else if (theUrl.StartsWith("?"))
            {
                theUrl = BasePage + theUrl;
            }
            if (!theUrl.StartsWith("http://") && !theUrl.StartsWith("https://"))
            {
                theUrl = BaseUrl + "/" + theUrl;
            }
            return theUrl;
        }

        /// <summary>
        /// 批量格式化url
        /// </summary>
        /// <param name="baseUrl"></param>
        /// <param name="urls"></param>
        /// <returns></returns>
        public List<string> FormatUrls(string baseUrl, List<string> urls)
        {
            List<string> reval = new List<string>();
            foreach (string newUrl in urls)
            {
                string url = FormatUrl(baseUrl, newUrl);
                reval.Add(url);
            }

            return reval;
        }

        /// <summary>
        /// 获得格式化后的url
        /// </summary>
        /// <param name="bsUrl">根url</param>
        /// <param name="sStr">源文件</param>
        /// <param name="Ptn">url正则表达式</param>
        /// <returns>格式化后的url</returns>
        public string GetUrl(string bsUrl, string sStr, string Ptn)
        {
            foreach (Match match in Regex.Matches(sStr, Ptn, RegexOptions.IgnoreCase))
            {
                string oldValue = match.Groups[0].Value;
                string str = match.Groups[3].Value;
                string newValue = oldValue.Replace(str, FormatUrl(bsUrl, str));
                sStr = sStr.Replace(oldValue, newValue);
            }
            return sStr;
        }

    }
}
